﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hosting.Class;
using System.Web.UI.HtmlControls;

namespace HostingDomainWebsite
{
    public partial class AdminPage : System.Web.UI.Page
    {

        protected SiteMaster master;

        protected void Page_PreLoad(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            master = Page.Master as SiteMaster;
            master.setCurrentNav("Account", "AdminPage");

            if (!IsPostBack)
            {
                dataRaw();
            }
        }

        /*
         * 1    : Successfully!
         * 30   : Email is not valid
         * 31   : Email is too big
         * 40   : Phone is null or not have value
         * 41   : Phone must be number
         * 42   : Phone is less than 10 character
         * 43   : Phone is bigger than 15 character
         * 50   : Address is too long
          */
        protected void Update_Click(object sender, EventArgs e)
        {

            Users users = new Users();
            int isValid = users.editValid(txtEmail.Value, txtPhone.Value, txtAddress.Value);
            if (isValid == 1)
            {
                int isUpdate = users.Edit(Session["username"].ToString(), txtEmail.Value, txtPhone.Value, txtAddress.Value, true);
                if (isUpdate == 0)
                {
                    master.getMessage("Data updates successfully!", "notification success png_bg");
                }
                else
                {
                    master.getMessage("There is an error!", "notification error png_bg");
                }
            }
            else if (isValid == 30)
            {
                master.getMessageAttention("Email is not valid");
            }
            else if (isValid == 31)
            {
                master.getMessageAttention("Email is too big");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("Phone is null or not have value");
            }
            else if (isValid == 41)
            {
                master.getMessageAttention("Phone must be number");
            }
            else if (isValid == 42)
            {
                master.getMessageAttention("Phone is less than 10 character");
            }
            else if (isValid == 43)
            {
                master.getMessageAttention("Phone is bigger than 15 character");
            }
            else if (isValid == 50)
            {
                master.getMessageAttention("Address is too long");
            }
        }

        protected void Reset_Click(object sender, EventArgs e)
        {
            dataRaw();
        }

        protected void dataRaw()
        {
            Users users = new Users();
            var u = users.getUser(Session["username"].ToString());
            if (u == null)
            {
                Response.Write("<script language='javascript'>alert('There something error when running, please login again!');</script>");
                Response.Redirect("AdminLogin.aspx");
            }
            else
            {
                if (u.ToList().Count > 0)
                {
                    foreach (var i in u)
                    {
                        txtEmail.Value = i.Email;
                        txtPhone.Value = i.Phone;
                        txtAddress.Value = i.Address;
                    }
                }
                else
                {
                    master.getMessageAttention("There are not any values");
                }
            }
        }

    }
}